<?php

/**
 * Description of topic
 *
 * @author b4by
 */
class page extends MY_Controller {

	function __construct() {
		parent::__construct();
		$this->load->model('model_page');
	}

	public function index() {
		$this->data['temp'] = 'admin/page/index';
		$this->load->view('admin/main', $this->data);
	}

	public function read() {
		$pagination = pagination_init($this->model_page->get_total(), request('p', 1));
		$pages = $this->model_page->get_list(array(
			'limit' => array($pagination->from, $pagination->limit),
				));
		foreach($pages as &$page)
		{
			$page->link = url_create('site','page','index',$page->id);
			$page->edit_link = url_create('admin','page','update',$page->id);
		}
		echo json_encode(array(
			'state' => 'success',
			'data' => $pages,
			'pagination' => $pagination,
		));
	}

	public function create() {

		if ($this->input->post('submit')) {

			$this->load->helper('form');
			$this->load->library('form_validation');

			$this->form_validation->set_rules('title', 'lang:page_title', 'required');
			$this->form_validation->set_rules('content', 'lang:page_content', 'required');
			$data = request_filter(array('title', 'content'));
			if ($this->validation_run()) {
				$this->model_page->create($data);
				redirect(url_create('admin', 'page', 'index'));
			}
		}
		$this->data['data'] = array();
		$this->data['module_title'] = lang('page_create');
		$this->data['module_title_desc'] = lang('page_create_desc');

		$this->data['temp'] = 'admin/page/detail';
		$this->load->view('admin/main', $this->data);
	}

	public function update($id) {
		$this->load->helper('form');
		$this->load->library('form_validation');
		$_POST['id'] = $id;
		$this->form_validation->set_rules('id', 'lang:page_id', 'required|callback__check_id');
		if ($this->input->post('submit')) {
			$this->form_validation->set_rules('title', 'lang:page_title', 'required');
			$this->form_validation->set_rules('content', 'lang:page_content', 'required');
		}
		$data = request_filter(array('title', 'content'));
		if ($this->validation_run()) {
			if ($this->input->post('submit')) {
				$this->model_page->update($id, $data);
				redirect(url_create('admin', 'page', 'index'));
			}
			$this->data['data'] = $this->model_page->get_info($id);
		}
		$this->data['module_title'] = lang('page_create');
		$this->data['module_title_desc'] = lang('page_create_desc');

		$this->data['temp'] = 'admin/page/detail';
		$this->load->view('admin/main', $this->data);
	}

	public function _check_id($id) {
		if ($this->model_page->get_total(array(
					'id' => $id
				)) != 1) {
			$this->form_validation->set_message(__FUNCTION__, lang('error'));
			return FALSE;
		}
		return TRUE;
	}

	public function delete() {
		$this->load->helper('form');
		$this->load->library('form_validation');
		$id = $this->input->post('id');
		$this->form_validation->set_rules('id', 'lang:page_id', 'required|callback__check_id');
		if ($this->form_validation->run()) {
			$this->model_page->del($id);
			echo json_encode(array(
				'state' => 'success',
				'id' => $id,
			));
		} else {
			json_encode(array(
				'state' => 'error',
				'title' => lang('error'),
				'message' => lang('page_not_exist')
			));
		}
	}
	
}

?>